var t = getApp();

Page({
    data: {
        imgUrl: "",
        TabCur: 0,
        scrollLeft: 0,
        emojiItems: [ "熊猫头", "蘑菇头", "红脸蛋", "其他" ],
        ad: {}
    },
    onShow(){
        this.getRandomAd()
        getApp().chaping()
    },
    onLoad: function() {
        getApp().getRandomAd(true,this)
        this.getRandomAd(true)
        wx.showLoading({
            title: "加载中"
        });
        var o = this;
        wx.request({
            url: "https://ishare20.net/emotion2/1.json",
            header: {
                "content-type": "application/json"
            },
            success: function(t) {
                wx.hideLoading(), o.setData({
                    imgUrl: t.data
                });
            },
            fail: function(t) {
                wx.showToast({
                    title: "连接出错，请稍后重试",
                    icon: "none",
                    duration: 2e3
                });
            }
        });
    },
    tabSelect: function(t) {
        this.getRandomAd()
        this.setData({
            TabCur: t.currentTarget.dataset.id
        }), wx.showLoading({
            title: "加载中"
        });
        var o = this;
        wx.request({
            url: "https://ishare20.net/emotion2/" + (t.currentTarget.dataset.id + 1) + ".json",
            header: {
                "content-type": "application/json"
            },
            success: function(t) {
                wx.hideLoading(), o.setData({
                    imgUrl: t.data
                });
            },
            fail: function(t) {
                wx.showToast({
                    title: "连接出错，请稍后重试",
                    icon: "none",
                    duration: 2e3
                });
            }
        });
    },
    chooseFromAlbum: function() {
        wx.chooseImage({
            count: 1,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album" ],
            success: async function(t) {
                var o = t.tempFilePaths[0];
                let imgData = await getApp().checkImg(t.tempFilePaths[0])
                console.log(imgData)
                if(!imgData||imgData.result.errCode!==0){
                    wx.showModal({
                    confirmText: '确定',
                    content: '图片内容不合法，请重新选择',
                    showCancel: false
                    })
                    return
                }
                wx.navigateTo({
                    url: "../emotionEdit/emotionEdit?choosedEmoUrl=" + o
                });
            }
        });
    },
    chooseEmotion: function(t) {
        var o = this.data.imgUrl[t.currentTarget.dataset.idx];
        wx.showLoading({
            title: "获取图片"
        }), wx.downloadFile({
            url: o,
            success: function(t) {
                wx.hideLoading(), wx.navigateTo({
                    url: "../emotionEdit/emotionEdit?choosedEmoUrl=" + t.tempFilePath
                });
            },
            fail: function(t) {
                wx.hideLoading(), wx.showToast({
                    title: "连接出错，请稍后重试",
                    icon: "none",
                    duration: 2e3
                });
            }
        });
    },
    onShareAppMessage: function() {
        return {
            title: "表情包制作-图片编辑助手",
            path: "pages/emotionChoose/emotionChoose"
        };
    }
});